package com.xzc.java8.B_Stream.i_practice;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

public class A_P1 {

    public static void main(String[] args) {
        List<Apple> list0 = new ArrayList<>();
        Apple a0 = new Apple("a0", "red", 150, 2011);
        Apple a1 = new Apple("a1", "blue", 10, 2012);
        Apple a2 = new Apple("a2", "red", 110, 2011);
        Apple a3 = new Apple("a3", "yellow", 140, 2013);
        list0.add(a0);
        list0.add(a1);
        list0.add(a2);
        list0.add(a3);

        //取出2011年的小于150克的苹果
        List<Apple> out1 = list0.stream().filter(a -> a.getYear() == 2011).filter(a -> a.getWeight() < 150).collect(toList());
        System.out.println(out1);

        //这箱苹果一共有几种颜色
        List<String> out2 = list0.stream().map(Apple::getColor).distinct().collect(toList());
        System.out.println(out2);

        //找到所有以a开头的苹果，并排序
        List<String> out3 = list0.stream().map(Apple::getName).filter(a -> a.startsWith("a")).sorted(Comparator.comparing(Apple::getNameCode)).collect(toList());
        System.out.println(out3);

        //将所有苹果按字母顺序排序
        List<String> out4 = list0.stream().map(Apple::getName).sorted(Comparator.comparing(Apple::getFullNameCode)).collect(toList());
        System.out.println(out4);

        //有没有b开头的苹果
        boolean out5 = list0.stream().anyMatch(a -> a.getName().startsWith("b"));
        System.out.println(out5);

        //打印出所有红苹果的重量
        list0.stream().filter(Apple::isRedApple).forEach((d) -> System.out.println(d.getWeight()));

        //最重的苹果是哪个
        Optional<Apple> out7 = list0.stream().max(Comparator.comparing(Apple::getWeight));
        out7.ifPresent(System.out::println);

        //最重的苹果,重量是多少
        Optional<Integer> out8 = list0.stream().map(Apple::getWeight).max(Integer::max);
        out8.ifPresent(System.out::println);
    }
}
